useEffectSkipMount
The useEffect
hook allows us to listen for changes to one or more variables, and run some sort of side effect when one of them changes.
The useEffect
hook also runs during the mount process, no matter what the dependency array is. There are plenty of situations where this isn't ideal, and we don't want the effect to run after the first render.
In this challenge, you'll create a new custom hook, useEffectSkipMount
. It should work just like React.useEffect
, except it should only run when the dependencies change, not after the initial render.
Difficulty
This is a very hard problem. Unless you have extensive React experience, you probably won't be able to solve it.
Rules
- You're not allowed to use any third-party libraries.
- Feel free to google anything you'd like.
Playground
To help you solve this problem, you've been given an application with two pieces of state:
count
, incremented by clicking a button.name
, a string bound to a controlled input.
The useEffectSkipMount
custom hook is being used to log any changes to both of these state variables; there are two separate effects. Your mission is to implement this hook so that it behaves just like useEffect
, but without logging the values during mount.
Acceptance Criteria:
useEffectSkipMount
should behave just likeReact.useEffect
, but without running during mount.- It should work correctly in Strict Mode.
Code Playground
My attempt
Phew! This is the final technical interview challenge, and it's a doozy. 😅
If you're looking for more practice, I'd suggest revisiting some of the exercises in the course! Most of them actually function quite well as interview challenges. You can get more practice thinking out loud, pretending that you're solving it for an audience.